<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加新闻-管理员</title>
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="${BasePath}/css/common.css?v=${cssVersion}">
<script type="text/javascript" src="${BasePath}/js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
<script type="text/javascript" src="${BasePath}/js/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script type="text/javascript" src="${BasePath}/js/ajaxHeader.js?v=${jsVersion}"></script>
<style>
</style>
</head>
<body>
	<div class="panel panel-default" style="width: 90%; margin:20px auto;">
		<div class="panel-heading">
	        <div class="panel-heading">
		        <div class="row" >
		        	<div class="col-lg-8">
			        	<strong>新闻添加（管理员端）</strong>
			        </div>
		        </div>
		    </div>
	    </div>
	    <div class="panel-body">
			<div role="form">
                <div class="form-group">
					<label>新闻标题</label>
					<input type="text" class="form-control" id="title" placeholder="标题...">
				</div>
				<div class="form-group">
					<label>新闻内容</label>
					<textarea class="form-control" id="content" placeholder="内容..."  rows="5" style="visibility:hidden"></textarea>
				</div>
				<div class="form-group">
					<label for="radionFile">新闻图片</label>
					<input type="file" id="imgFiles" multiple accept="image/*">
				</div>
				<button class="btn btn-primary btnAddNews" data-loading-text="拼命上传中...">立即添加</button>
			</div>
	    </div>
	</div>
	<div class="prompt"></div>
	<#include '/include/baseJs.html'>
	<script type="text/javascript" src="${BasePath}/js/common-func.js?v=${jsVersion}"></script>
	<script>
	//禁用返回
//     window.history.pushState(null, null, "#");
// 	window.addEventListener("popstate", function(e) {
// 		window.history.pushState(null, null, "#");
// 	});

var itemEditor;
    $(function(){
		itemEditor=createEditor($("#content"))
	})
	$(".btnAddNews").click(function(){
		var $btn = $(this);
		// 同步数据后可以直接取得textarea的value
		itemEditor.sync();
		var params = {
			title: $("#title").val(),
			content: $("#content").val(),
		};
		
		console.log(params)
		if(isNull(params.title)){
			prompt($(".prompt"), '请输入标题！');
			return;
		}
		if(isNull(params.content)){
			prompt($(".prompt"), '请输入新闻内容！');
			return;
		}
		
		if($("#imgFiles").val() == ""){
			showMsg('请选择图片！');
			return;
		}
		
		var formData = new FormData();
        formData.append("requestData", JSON.stringify(params));
        
        var files = $("#imgFiles")[0].files;
        for(var i = 0; i < files.length; ++i){
        	formData.append("files", files[i]);
        }
        $.ajax({
            url : "${BasePath}/api/news/add", 
            type : 'POST', 
            data : formData, 
            processData : false, // 告诉jQuery不要去处理发送的数据
            contentType : false, // 告诉jQuery不要去设置Content-Type请求头
            beforeSend:function(){
            	$btn.button('loading');
            },
            success: function(data) {
            	$btn.button('reset');
            	console.log(data);
            	if(data == null) return;
				if(data.code == 200){
					var ret = Boolean(data.responseData);
					if(ret){
						alert("操作成功！");
						location.href = "${BasePath}/page/admin/news";
						return;
					}
				}
				alert("操作失败！");
            }, 
            error : function(err) { 
            	$btn.button('reset');
                var errMsg = "操作出错，详情：" + JSON.stringify(err);
                console.log(errMsg);
                alert(errMsg);
            } 
        }); 
	});
    
    var kingEditorParams={
		filePostName:"uploadFile",
		uploadJson:'/pic/upload',
		dir : "image"
	}
	
	function createEditor(select){
		return KindEditor.create(select,kingEditorParams)
	}
	</script>
</body>
</html>